package com.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.health.pojo.CheckItem;
import com.health.service.CheckItemService;
import com.health.vo.PageResult;
import com.health.vo.Result;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkItem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    /**
     * 检查项条件查询
     */
    @GetMapping("/findByQuery")
    public Result findByQuery(String query) {
        List<CheckItem> checkItemList = checkItemService.findByQuery(query);

        //封装返回结果
        return new Result(true, "查询检查项成功", checkItemList);
    }

    /**
     * 编辑检查项
     */
    @PostMapping("/update")
    public Result update(@RequestBody CheckItem checkItem) {
        int count = checkItemService.update(checkItem);
        if (count > 0) {
            return new Result(true, "编辑检查项成功");
        }

        return new Result(false, "编辑检查项失败");
    }

    /**
     * 根据ID查询检查项
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {
        CheckItem checkItem = checkItemService.findById(id);

        //封装返回结果
        return new Result(true, "检查项查询成功", checkItem);
    }

    /**
     * 新增检查项
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) {
        int count = checkItemService.add(checkItem);
        if (count > 0) {
            return new Result(true, "新增检查项成功");
        }

        return new Result(false, "新增检查项失败");
    }

    /**
     * 删除检查项
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        int count = checkItemService.deleteById(id);
        if (count > 0) {
            return new Result(true, "删除检查项成功");
        }

        return new Result(false, "删除检查项失败");
    }

    /**
     * 检查项分页条件查询
     */
    @GetMapping("/findPage/{pageNow}/{pageSize}")
    public Result findPage(@PathVariable Integer pageNow, @PathVariable Integer pageSize, String query) {
        PageResult<CheckItem> pageResult = checkItemService.findPage(pageNow, pageSize, query);

        //封装返回结果
        return new Result(true, "检查项分页查询成功", pageResult);
    }

    /**
     * 查询所有检查项
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckItem> checkItemList = checkItemService.findAll();

        //封装返回结果
        return new Result(true, "查询所有检查项成功", checkItemList);
    }
}